package com.stu.Controller;

import com.stu.Pojo.DePartment;
import com.stu.Pojo.Employee;
import com.stu.mapper.EmployeeMapper;
import com.stu.service.EmployeeService;
import com.stu.service.IDeperatmentService;
import com.stu.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
//控制层
@RequestMapping("/emp")
public class EmployeeContorller {
    @Autowired
    //注入 业务层的bean
    private EmployeeService employeeService;
    @CrossOrigin
    @GetMapping

    //设置查询  以及参数
    public List<Employee> queryall(){
        return  employeeService.selectALL();
        //调用业务层
    }
    @GetMapping("/{id}")
    public  Employee queryByid(@PathVariable("id")Long id)
    {
        return  employeeService.selectByid(id);
    }
    @PutMapping
    public AjaxResult insertorupdate(@RequestBody Employee employee){
        try {
            if (employee.getId()==null){
                employeeService.insert(employee);
            }else {
                employeeService.update(employee);
            }
        }catch (Exception e){
            e.printStackTrace();
            return  new AjaxResult(false,"操作失败");
        }
       return new AjaxResult(true,"操作成功");
    }
}
